<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Constants\ContactSourceConstants;
use App\CustHub\Constants\ContactTaskCenterStatusConstants;
use App\CustHub\Constants\ContactTaskCenterTyperConstants;
use App\CustHub\Request\ContactRequest;
use App\CustHub\Service\ChContactService;
use App\CustHub\Service\ChContactTaskCenterService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: 'ch/contact'),ApiAuth,VerifySignature]
class ContactController extends BaseController
{

    public array $noNeedLogin = ['export'];
    public array $noNeedVerifySign = ['export'];

    #[Inject]
    protected ChContactService $service;


    /**
     * 获取客户列表
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('list'),ChPermission("ch:contact:list")]
    public function list(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getContactList($this->request->all()));
    }

    /**
     * 加入黑名单
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('addToBlacklist')]
    public function addToBlacklist(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->addToBlacklist($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 获取客户详情
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getInfo')]
    public function getInfo(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getInfo($request->all()));
    }

    /**
     * 获取当前客户所在的群聊
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getUserRooms')]
    public function getUserRooms(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getUserRooms($request->all()));
    }

    /**
     * 行为日志
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('behaviorLog')]
    public function behaviorLog(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->behaviorLog($request->all()));
    }


    /**
     * 同步客户信息
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('syncContact')]
    public function syncContact(): \Psr\Http\Message\ResponseInterface
    {
        $this->service->syncContact($this->request->all());
        return $this->success();
    }

    /**
     * 获取客户来源
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getSourceList')]
    public function getSourceList(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success(ContactSourceConstants::getOptions());
    }


    /**
     * 修改客户备注
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('editRemark')]
    public function editRemark(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->editRemark($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 修改手机号
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('editMobile')]
    public function editMobile(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->editMobile($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 修改客户描述
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('editDesc')]
    public function editDesc(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->editDesc($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 修改公司备注
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('editCompanyRemark')]
    public function editCompanyRemark(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->editCompanyRemark($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 设置称呼
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('editCallName')]
    public function editCallName(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->editCallName($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 设置企微标签
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('setCorpTag')]
    public function setCorpTag(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->setCorpTag($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 批量设置企微标签
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('batchSetCorpTag')]
    public function batchSetCorpTag(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->batchSetCorpTag($request->all()) ? $this->success('批量操作任务提交成功，可在 “任务中心” 查看进度') : $this->error();
    }

    /**
     * 批量移除企微标签
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('batchRemoveCorpTag')]
    public function batchRemoveCorpTag(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->batchRemoveCorpTag($request->all()) ? $this->success('批量操作任务提交成功，可在 “任务中心” 查看进度') : $this->error();
    }

    /**
     * 设置个人标签
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('setPersonalTag')]
    public function setPersonalTag(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->setPersonalTag($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 设置智能标签
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('setSmartTag')]
    public function setSmartTag(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        $data['ids'] = json_encode(['id'=>$request->input('id')]);
        return $this->service->batchSetSmartTag($data) ? $this->success() : $this->error();
    }

    /**
     * 批量设置智能标签
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('batchSetSmartTag')]
    public function batchSetSmartTag(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->batchSetSmartTag($request->all()) ? $this->success('批量操作任务提交成功，可在 “任务中心” 查看进度') : $this->error();
    }

    /**
     * 批量移除智能标签
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('batchRemoveSmartTag')]
    public function batchRemoveSmartTag(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->batchRemoveSmartTag($request->all()) ? $this->success('批量操作任务提交成功，可在 “任务中心” 查看进度') : $this->error();
    }

    /**
     * 批量移除智能标签
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('batchEditRemark')]
    public function batchEditRemark(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->batchEditRemark($request->all()) ? $this->success('批量操作任务提交成功，可在 “任务中心” 查看进度') : $this->error();
    }


    /**
     * 删除
     * @param ContactRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('delete')]
    public function delete(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->deleteContact($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 导出
     * @return \Psr\Http\Message\ResponseInterface
     */
    #[GetMapping('export')]
    public function export(): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->exportContacts($this->request->all());
    }

    /**
     * 任务中心
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('tcList')]
    public function tcList(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success(container()->get(ChContactTaskCenterService::class)->list($this->request->all()));
    }

    #[PostMapping('tcConfig')]
    public function tcConfig(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success([
            'typeList' => ContactTaskCenterTyperConstants::getOptions(),
            'statusList' => ContactTaskCenterStatusConstants::getOptions(),
        ]);
    }

    #[PostMapping('cancelTask')]
    public function cancelTask(ContactRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return container()->get(ChContactTaskCenterService::class)->cancelTask($request->all()) ? $this->success() : $this->error();
    }
}